﻿using IMModel.Domain.Common;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System;
namespace IMModel.Domain
{
    //class DefaultAnswerVolumePolicy : IAnswerVolumePolicy
    //{
    //    public static readonly DefaultAnswerVolumePolicy Instance = new DefaultAnswerVolumePolicy();


    //}

    /// <summary>
    /// Стратегия расчёта индексов по умолчанию
    /// </summary>
    public class DefaultAnswerVolumePolicy : IAnswerVolumePolicy
    {
        public static readonly DefaultAnswerVolumePolicy Instance = new DefaultAnswerVolumePolicy();

        /// <summary>
        /// Расчитывает индексы коллекции с элементами типа IRespondentIndexCollector: I' = Sum(I*W) / Max(I*W)
        /// </summary>
        /// <param name="сontext">Контекст - переносчик</param>
        /// <param name="currCharactericTerm">Показатель в периоде</param>
        /// <returns>Коллекция индексов</returns>
        public RespondentIndexCollection GetIndexes(IndexContext сontext,
            CharacteristicTerm currCharactericTerm)
        {
            Contract.Requires(currCharactericTerm != null);
            var term = сontext.Term;
            var c = new RespondentIndexCollection();
            while (currCharactericTerm != null)
            {
                if (currCharactericTerm.IsValidInPeriod(term))
                {
                    c += currCharactericTerm.GetTermVolumes(сontext, currCharactericTerm, term);
                }
                currCharactericTerm = currCharactericTerm.InsteadOf;
            }
            return c;
        }
    }

    /// <summary>
    /// Стратегия расчёта индексов для докторов наук
    /// </summary>
    public class DoctorAnswerVolumePolicy : IAnswerVolumePolicy
    {
        public static readonly DoctorAnswerVolumePolicy Instance = new DoctorAnswerVolumePolicy();
        public double[] Weights = new double[] { 1 };

        /// <summary>
        /// Расчитывает индексы коллекции с элементами типа IRespondentIndexCollector: I' = Sum(I*W) / Max(I*W)
        /// </summary>
        /// <param name="сontext">Контекст - переносчик</param>
        /// <param name="currCharactericTerm">Показатель в периоде</param>
        /// <returns>Коллекция индексов</returns>
        public RespondentIndexCollection GetIndexes(IndexContext сontext,
            CharacteristicTerm currCharactericTerm)
        {
            Contract.Requires(currCharactericTerm != null);
            var term = сontext.Term;
            var c = new RespondentIndexCollection();
            int termOrder = term.Order, order;
            int max = Weights.Length;

            while (currCharactericTerm != null)
            {
                // убывание по периодам
                order = termOrder - currCharactericTerm.Term.Order;
                if (order < max)
                {
                    c += currCharactericTerm.GetTermVolumes(сontext, currCharactericTerm, term) * Weights[order];
                    currCharactericTerm = currCharactericTerm.InsteadOf;
                }
                else break;
            }
            return c;
        }
    }
}